package com.lankystar.todolist.page.account;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.lankystar.common.Page;
import com.lankystar.common.Result;
import com.lankystar.todolist.model.Account;
import com.lankystar.todolist.model.Role;
import com.lankystar.todolist.service.IAccountService;
import com.lankystar.todolist.service.ITaskService;

@Controller
@RequestMapping("/account")
public class AccountAction {
	@Autowired
    private ITaskService taskService;
	@Autowired
	private IAccountService accountService;
	@RequestMapping("/toLogin")
	public ModelAndView toLogin() {
		return new ModelAndView("account/layout/login.html");
	}
	@RequestMapping("/toFrame")
	public ModelAndView toFrame() {
		return new ModelAndView("account/layout/Frame.html");
	}
	@RequestMapping("/toRegister")
	public ModelAndView toRegister() {
		return new ModelAndView("account/layout/register.html");
	}
	@RequestMapping("/toTop")
	public ModelAndView toTop() {
		return new ModelAndView("account/layout/top.html");
	}
	
	@RequestMapping("/toLink")
	public ModelAndView toLink() {
		return new ModelAndView("account/layout/link.html");
	}
	@RequestMapping("/toMain")
	public ModelAndView toMain(HttpServletRequest req,Page<Map<String,String>> page) {
		Page<Map<String, String>> taskpage=taskService.getTasks(page);
		List<Map<String, String>> tasklist=taskpage.getDatas();
		req.setAttribute("tasklist",tasklist);
		return new ModelAndView("account/layout/main.html");
	}
	
	@RequestMapping("/toAddUser")
	public ModelAndView toAddUser() {
		return new ModelAndView("account/layout/addUser.html");
	}
	
	@RequestMapping("/toModify")
	public ModelAndView toModify(HttpServletRequest req,Integer id,Integer i) {
		Account account = accountService.selectAccountById(id);
		req.setAttribute("account",account);
		if(i==null){
			return new ModelAndView("account/layout/modifyRole.html");
		}else{
			return new ModelAndView("account/layout/modify.html");
		}
	}
	
	@RequestMapping("/delete")
	public void delete(Integer id,HttpServletResponse resp){
		accountService.deleteById(id);
		try {
			/**
			 * 重定向请求
			 */
			resp.sendRedirect("toUser");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 登陆
	 * 
	 * @param info
	 * @param username
	 * @param userpwd
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "/loginAction",method=RequestMethod.POST)
	@ResponseBody
	public Result<String> login(String username, String userpwd, HttpServletRequest req) {
		Result<String> result = new Result<>();
		if("".equals(userpwd)||"".equals(username)){
			result.setCode(Result.FAIL);
			result.setMsg("账户名，密码不能为空！！");
			return result;
		}else{
			Account account = accountService.login(username, userpwd);
			if (account != null){
			//	String role=accountService.getTaskRole(account.getId());
				req.getSession().setAttribute("user", account);
				//req.getSession().setAttribute("role", role);
				result.setCode(Result.OK);
				return result;
			}else{
				result.setCode(Result.FAIL);
				result.setMsg("对不起，账户名密码不匹配，请重新登录!!");
				return result;
			}
		}
	}

	/**
	 * 注册
	 * 
	 * @param info
	 * @param username
	 * @param userpwd
	 * @param realpwd
	 * @return
	 */
	@RequestMapping("/registerAction")
	public Result<String> register(String username, String userpwd, String nickname,
			Integer role, String realpwd) {
		Result<String> result= new Result<>();
		Role r = Role.values()[role];
		if("".equals(username)||"".equals(userpwd)||"".equals(nickname)||"".equals(r)||"".equals(realpwd)){
			result.setCode(Result.FAIL);
			result.setMsg("账户信息不能为空！！");
			return result;
		}else{
			Account account = accountService.login(username, userpwd);
			if(account!=null){
				result.setCode(Result.FAIL);
				result.setMsg("账户名已存在！！");
				return result;
			}else{
				if(userpwd.equals(realpwd)){
					account = new Account();
					account.setUsername(username);
					account.setUserpwd(userpwd);
					account.setNickname(nickname);
					account.setRole(r.toString());
					accountService.register(account);
					result.setCode(Result.OK);
					return	result;
				}else{
					result.setCode(Result.FAIL);
					result.setMsg("前后密码不一致!!");
					return result;
				}
			}
			
		}
	}

	/**
	 * 账户信息更新
	 * success
	 * @param req
	 * @param userpwd
	 * @param confirm
	 * @return
	 */
	@RequestMapping(value="/modify",method=RequestMethod.POST)
	@ResponseBody
	public Result<String> modify(String username, String userpwd, String nickname,
			Integer userId,Integer role) {
		Result<String> result = new Result<>();
		Account account = accountService.selectAccountById(userId);
		if (account==null) {
			result.setCode(Result.FAIL);
			result.setMsg("此账户不存在！！");
			return result;
		}else{
			/**
			 * 角色是否修改
			 */
			if(role==null){
				account.setRole(accountService.selectAccountById(userId).getRole());
			}else{
				Role r = Role.values()[role];
				account.setRole(r.toString());
			}
			account.setUsername(username);
			account.setUserpwd(userpwd);
			account.setNickname(nickname);
			accountService.modify(account);
			result.setCode(Result.OK);
			return result;
		}

	}

	@RequestMapping(value="/addUserAction",method=RequestMethod.POST)
	@ResponseBody
	public Result<String> addUser(String username, String userpwd, String nickname,
			Integer role) {

		Role r = Role.values()[role];
		Result<String> result= new Result<>();
		if("".equals(username)||"".equals(userpwd)||"".equals(nickname)||"".equals(r)){
			result.setCode(Result.FAIL);
			result.setMsg("账户信息不能为空！！");
			return result;
		}else{
			Account account = accountService.login(username, userpwd);
			if(account!=null){
				result.setCode(Result.FAIL);
				result.setMsg("账户名已存在！！");
				return result;
			}else{
					account = new Account();
					account.setUsername(username);
					account.setUserpwd(userpwd);
					account.setNickname(nickname);
					account.setRole(r.toString());
					accountService.register(account);
					result.setCode(Result.OK);
					return	result;
			}
			
		}

	}
	
	@RequestMapping(value="/toUser")
	public ModelAndView toUser(HttpServletRequest req) {
		List<Account> accountlist = accountService.selectAccounts();
		Account record = (Account) req.getSession().getAttribute("user");
		List<Object> list = new ArrayList<>();
		for (Account account : accountlist) {
			/**
			 * 不展示权限高于或等于登陆者的用户
			 */
			if("SUPERADMIN".equals(record.getRole())){
				if("SUPERADMIN".equals(account.getRole())){
					list.add(account);
				}
			}
			if("ADMIN".equals(record.getRole())){
				if("ADMIN".equals(account.getRole())||"SUPERADMIN".equals(account.getRole())){
					list.add(account);
				}
			}
		}
		accountlist.removeAll(list);
		req.setAttribute("accountlist", accountlist);
		return new ModelAndView("account/layout/user.html");
	}
}
